Skip to content

[dotnet-linker] Set AssociatedSourceType on UnmanagedCallersOnly trampolines - #25915

Closed
rolfbjarne wants to merge 5 commits into
net11.0from
dev/rolf/unmanagedcallersonly-associatedsourcetype
Closed

rolfbjarne wants to merge 5 commits into
net11.0from
dev/rolf/unmanagedcallersonly-associatedsourcetype

Conversation

@rolfbjarne

@rolfbjarne rolfbjarne commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Set the AssociatedSourceType field on the UnmanagedCallersOnly attributes the managed registrar emits, so the NativeAOT compiler (ILC) can drop a trampoline's native export when the associated type (the NSObject subclass declaring the exported method) can't be constructed. This is an app-size optimization for NativeAOT (issue #25631).

Fixes #25631

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53

Copilot AI review requested due to automatic review settings July 2, 2026 18:09
@rolfbjarne rolfbjarne added copilot do-not-merge Do not merge this pull request labels Jul 2, 2026
@rolfbjarne

This comment was marked as outdated.

@rolfbjarne
rolfbjarne marked this pull request as draft July 2, 2026 18:10
@rolfbjarne rolfbjarne linked an issue Jul 2, 2026 that may be closed by this pull request

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the managed registrar’s generated UnmanagedCallersOnly trampolines to set AssociatedSourceType, so NativeAOT/trimming can elide entry points when the corresponding NSObject subclass is trimmed, reducing app size.

Changes:

  • Pass the exported method’s declaring type into CreateUnmanagedCallersAttribute.
  • Emit an AssociatedSourceType named argument on generated UnmanagedCallersOnlyAttribute instances.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne force-pushed the dev/rolf/unmanagedcallersonly-associatedsourcetype branch from ead51c2 to c905f4a Compare July 16, 2026 17:08
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne added a commit that referenced this pull request Jul 22, 2026
…tocol on net11.0.

The trimmable static registrar's .NET 11 TypeMap path (--typemap-entry-assembly)
preserves the full exported method surface of registered framework types that the
app subclasses (e.g. SceneDelegate : UIResponder, AppDelegate : UIApplicationDelegate).
That keeps methods like UIResponder.PressesBegan (which takes an NSSet<UIPress>),
UIResponder.AccessibilityAssistiveTechnologyFocusedIdentifiers (NSSet<NSString>) and
UIApplicationDelegate.DidDiscardSceneSessions (NSSet<UISceneSession>) alive, which in
turn keeps NSSet<T> alive - so it isn't trimmed away and this test fails.

The [ProtocolMember] attribute trimming this test was written for still works
correctly (no ProtocolMemberAttribute instances remain in the linked assemblies);
NSSet<T> is simply kept alive by these real methods instead.

This is fixed by #25915, so ignore the test
until that PR is merged. The Assert.Ignore is placed so that it produces a merge
conflict with the corresponding marker in #25915, forcing us to re-enable the test
when resolving the conflict.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a19e8dea-25aa-4e58-a63c-2f2e161164ed
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

…ar after ILC

Set the AssociatedSourceType field on the UnmanagedCallersOnly attributes the
managed registrar emits, so the NativeAOT compiler (ILC) can drop a trampoline's
native export when the associated type (the NSObject subclass declaring the
exported method) can't be constructed. This is an app-size optimization for
NativeAOT (issue #25631).

The field is only emitted for NativeAOT + the trimmable-static registrar +
PrepareAssemblies:

* Only ILC understands the field; CoreCLR's attribute parser rejects any
  UnmanagedCallersOnly named argument it doesn't recognize ("Known custom
  attribute named argument not recognized.") at startup, so it must not be
  emitted for CoreCLR/Mono.
* The trimmable-static registrar emits native glue (registrar.o) that references
  the trampoline symbols directly for the whole set of registered SDK types.
  ILLink keeps those types (so the glue is emitted), but ILC independently drops
  some trampoline exports, which would leave undefined symbols at native link
  time. The fix requires a post-ILC step that can reconcile the generated glue
  with the exports that survived ILC, which only PrepareAssemblies has.

To make this work with the trimmable-static registrar, the native
registrar-generating postprocessing pipeline is reordered to run *after* ILC
instead of before (gated on trimmable-static + NativeAOT + PrepareAssemblies).
This is possible because ILLink is skipped in this scenario (ILC consumes the
prepared assemblies directly), so the postprocessing output no longer feeds ILC.

After ILC, the registrar inspects ILC's output object file to determine which
trampolines survived:

* Trampolines ILC trimmed away are routed through the dlsym fallback (a runtime
  string reference) instead of a direct native symbol reference. Since ILC only
  trims a trampoline when the associated type can't be constructed, the trampoline
  is never actually invoked, so the fallback is never reached.
* Classes whose trampolines were all trimmed away are skipped entirely (no
  @interface/@implementation is emitted), except base classes still referenced as
  a superclass of an emitted class (the whole base-class chain is kept).

Additionally, a warning is emitted if any post-ILC postprocessing step modifies a
managed assembly, since ILC has already consumed them and the change would be
silently lost. Two spurious "modifications" are fixed to avoid false warnings:
ManagedRegistrarStep.ProcessType now reports the actual modification state (during
post-processing it only collects information, a read-only operation), and the
NSObject.RegisterToggleRef stub (already applied before ILC) is no longer
re-applied post-ILC.

Fixes #25631

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53
@rolfbjarne
rolfbjarne force-pushed the dev/rolf/unmanagedcallersonly-associatedsourcetype branch from 29e1862 to b1131da Compare July 23, 2026 06:02
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [CI Build #a04ce3e] Prepare .NET Release succeeded ✅

📦 Published NuGet packages (32 packages)

iOS

  • Microsoft.iOS.Ref.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Runtime.ios-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Runtime.ios.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Runtime.iossimulator-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Runtime.iossimulator-x64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Sdk.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Templates.26.5.11989-net11-p7.nupkg
  • Microsoft.iOS.Windows.Sdk.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.NET.Sdk.iOS.Manifest-11.0.100-preview.7.26.5.11989-net11-p7.nupkg

MacCatalyst

  • Microsoft.MacCatalyst.Ref.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.MacCatalyst.Runtime.maccatalyst-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.MacCatalyst.Runtime.maccatalyst-x64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.MacCatalyst.Runtime.maccatalyst.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.MacCatalyst.Sdk.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.MacCatalyst.Templates.26.5.11989-net11-p7.nupkg
  • Microsoft.NET.Sdk.MacCatalyst.Manifest-11.0.100-preview.7.26.5.11989-net11-p7.nupkg

macOS

  • Microsoft.macOS.Ref.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.macOS.Runtime.osx-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.macOS.Runtime.osx-x64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.macOS.Runtime.osx.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.macOS.Sdk.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.macOS.Templates.26.5.11989-net11-p7.nupkg
  • Microsoft.NET.Sdk.macOS.Manifest-11.0.100-preview.7.26.5.11989-net11-p7.nupkg

tvOS

  • Microsoft.NET.Sdk.tvOS.Manifest-11.0.100-preview.7.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Ref.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Runtime.tvos-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Runtime.tvos.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Runtime.tvossimulator-arm64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Runtime.tvossimulator-x64.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Sdk.net11.0_26.5.26.5.11989-net11-p7.nupkg
  • Microsoft.tvOS.Templates.26.5.11989-net11-p7.nupkg

Other

  • Sharpie.Bind.Tool.26.6.0.593-net11-p7.nupkg

Pipeline on Agent
Hash: a04ce3e59e7cdacfc2d9dfd6af6f75971be0841a [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

…SSet<T> alive

The test verifies that a generic type referenced only from an optional protocol
member (Foundation.NSSet<T>) is trimmed away when using the trimmable static
registrar. It did so with:

    typeof (NSObject).Assembly.GetType (NamespacePrefix + "Foundation.NSSet`1")

Since NamespacePrefix is a `const string = ""`, the argument is a compile-time
constant, so ILLink's intrinsic handling of Assembly.GetType(string) resolves it
and marks Foundation.NSSet`1 as kept - the test's own assertion was the sole
reason the type survived (confirmed via --dump-dependencies). The result was the
assertion failing with "NSSet<T> must be linked away, otherwise this test is
useless".

Fix it the same way the sibling RemovedAttributes test already does: copy the
name pieces into non-const locals and go through Helper.GetType, so ILLink can't
statically resolve the type name and keep it. With this change NSSet<T> is
trimmed and the test passes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne marked this pull request as ready for review July 27, 2026 18:11
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne removed the do-not-merge Do not merge this pull request label Jul 28, 2026
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 6f9ce958945e4f491b9f2aba834fa332133dc33e [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #6f9ce95] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 12 tests failed, 244 tests passed.

Failures

❌ linker tests (iOS)

4 tests failed, 27 tests passed.

Failed tests

  • link all/iOS - simulator/Debug (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed
  • link all/iOS - simulator/Debug (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed
  • link all/iOS - simulator/Release (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed
  • link all/iOS - simulator/Release (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed

Html Report (VSDrops) Download

❌ linker tests (MacCatalyst)

4 tests failed, 27 tests passed.

Failed tests

  • link all/Mac Catalyst/Debug (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed (Test run failed.
    Tests run: 95 Passed: 86 Inconclusive: 0 Failed: 1 Ignored: 8)
  • link all/Mac Catalyst/Debug (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed (Test run failed.
    Tests run: 95 Passed: 86 Inconclusive: 0 Failed: 1 Ignored: 8)
  • link all/Mac Catalyst/Release (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed (Test run failed.
    Tests run: 95 Passed: 86 Inconclusive: 0 Failed: 1 Ignored: 8)
  • link all/Mac Catalyst/Release (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed (Test run failed.
    Tests run: 95 Passed: 86 Inconclusive: 0 Failed: 1 Ignored: 8)

Html Report (VSDrops) Download

❌ linker tests (tvOS)

4 tests failed, 27 tests passed.

Failed tests

  • link all/tvOS - simulator/Debug (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed
  • link all/tvOS - simulator/Debug (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed
  • link all/tvOS - simulator/Release (PrepareAssemblies, CoreCLR, Trimmable Static Registrar): Failed
  • link all/tvOS - simulator/Release (PrepareAssemblies, MonoVM, Trimmable Static Registrar): Failed

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 7 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 23 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 23 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 23 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 6f9ce958945e4f491b9f2aba834fa332133dc33e [PR build]

@rolfbjarne

Copy link
Copy Markdown
Member Author

Incorporated into #26408.

@rolfbjarne rolfbjarne closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Start using UnmanagedCallersOnlyAttribute.AssociatedSourceType

3 participants